package com.wx.sys.shiro;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import org.apache.shiro.cache.Cache;
import org.apache.shiro.cache.CacheException;
import org.apache.shiro.cache.CacheManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.wx.base.util.jedis.JedisManager;

public class JedisCacheManaer implements CacheManager {
	private static final Logger logger = LoggerFactory.getLogger(JedisCacheManaer.class);
	// fast lookup by name map
	private final ConcurrentMap<String, Cache> caches = new ConcurrentHashMap<String, Cache>();

	private JedisManager jedisManager;
	private String keyPrefix = "shiro_redis_cache:";

	public <K, V> Cache<K, V> getCache(String name) throws CacheException {
		Cache cache = caches.get(name);
		if (cache == null) {
			cache = new JedisCache<K, V>(jedisManager, keyPrefix);
			caches.put(name, cache);
		}
		return cache;
	}

	public JedisManager getJedisManager() {
		return jedisManager;
	}

	public void setJedisManager(JedisManager jedisManager) {
		this.jedisManager = jedisManager;
	}

	public String getKeyPrefix() {
		return keyPrefix;
	}

	public void setKeyPrefix(String keyPrefix) {
		this.keyPrefix = keyPrefix;
	}

}
